package cn.lblbc.downloadlib;

import android.os.Environment;

/**
 * 下载配置类
 */
public class DownloadConfig {
    private final int concurrentTaskNum;  // 同时下载的任务数（默认：3）
    private final int maxChunksPerFile;   // 单个文件最大分片数（默认：4，优先级高于 chunkSize）
    private final String defaultSaveDir;  // 默认存储目录
    private final boolean enableChunkDownload; // 是否启用分片下载
    private final long chunkSize;         // 单个分片的大小（字节，默认：2MB = 2 * 1024 * 1024）

    private DownloadConfig(Builder builder) {
        this.concurrentTaskNum = builder.concurrentTaskNum;
        this.maxChunksPerFile = builder.maxChunksPerFile;
        this.defaultSaveDir = builder.defaultSaveDir;
        this.enableChunkDownload = builder.enableChunkDownload;
        this.chunkSize = builder.chunkSize;
    }

    public int getConcurrentTaskNum() {
        return concurrentTaskNum;
    }

    public int getMaxChunksPerFile() {
        return maxChunksPerFile;
    }

    public String getDefaultSaveDir() {
        return defaultSaveDir;
    }

    public boolean isEnableChunkDownload() {
        return enableChunkDownload;
    }

    public long getChunkSize() {
        return chunkSize;
    }

    public static class Builder {
        private int concurrentTaskNum = 3;
        private int maxChunksPerFile = 4;
        private String defaultSaveDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
        private boolean enableChunkDownload = true;
        private long chunkSize = 2 * 1024 * 1024; // 默认 2MB

        public Builder setConcurrentTaskNum(int num) {
            this.concurrentTaskNum = num;
            return this;
        }

        public Builder setMaxChunksPerFile(int num) {
            this.maxChunksPerFile = num;
            return this;
        }

        public Builder setDefaultSaveDir(String dir) {
            this.defaultSaveDir = dir;
            return this;
        }

        public Builder setEnableChunkDownload(boolean enable) {
            this.enableChunkDownload = enable;
            return this;
        }

        public Builder setChunkSize(long size) {
            this.chunkSize = size;
            return this;
        }

        public DownloadConfig build() {
            return new DownloadConfig(this);
        }
    }
}